class Admin::ArtworkDescriptionsController < Admin
  # GET /artwork_descriptions
  # GET /artwork_descriptions.xml
  def index
    @artwork_descriptions = ArtworkDescription.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @artwork_descriptions }
    end
  end

  # GET /artwork_descriptions/1
  # GET /artwork_descriptions/1.xml
  def show
    @artwork_description = ArtworkDescription.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @artwork_description }
    end
  end

  # GET /artwork_descriptions/new
  # GET /artwork_descriptions/new.xml
  def new
    @artwork_description = ArtworkDescription.new
    @artwork = Artwork.find(params[:artwork_id])

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @artwork_description }
    end
  end

  # GET /artwork_descriptions/1/edit
  def edit
    @artwork_description = ArtworkDescription.find(params[:id])
  end

  # POST /artwork_descriptions
  # POST /artwork_descriptions.xml
  def create
    @artwork_description = ArtworkDescription.new(params[:artwork_description])
    artwork = Artwork.find(@artwork_description.artwork_id)
    artwork.last_edited_by = current_user
    artwork.save
    
    respond_to do |format|
      if @artwork_description.save
        flash[:notice] = 'Artwork description(s) successfully created.'
        format.html { redirect_to(new_media_url(:artwork_id => @artwork_description.artwork.id)) }
        format.xml  { render :xml => @artwork_description, :status => :created, :location => @artwork_description }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @artwork_description.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /artwork_descriptions/1
  # PUT /artwork_descriptions/1.xml
  def update
    @artwork_description = ArtworkDescription.find(params[:id])
    artwork = Artwork.find(@artwork_description.artwork_id)
    artwork.last_edited_by = current_user
    artwork.save
    
    old_language, old_description = @artwork_description.language, @artwork_description.description

    respond_to do |format|
      if @artwork_description.update_attributes(params[:artwork_description])
        flash[:notice] = 'ArtworkDescription was successfully updated.'
        format.html { redirect_to(@artwork_description) }
        format.xml  { head :ok }
        format.js { render :update do |page|
        if old_language != @artwork_description.language
            page.visual_effect :highlight, 'language'
        end
        if old_description != @artwork_description.description
          page.visual_effect :highlight, 'description'
        end
   
      end}
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @artwork_description.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /artwork_descriptions/1
  # DELETE /artwork_descriptions/1.xml
  def destroy
    @artwork_description = ArtworkDescription.find(params[:id])
    @artwork_description.destroy
    @artwork_description.artwork.unpublish_if_necessary
    
    artwork = Artwork.find(@artwork_description.artwork_id)
    artwork.last_edited_by = current_user
    artwork.save

    respond_to do |format|
      format.html { redirect_to(artwork_descriptions_url) }
      format.xml  { head :ok }
      format.js { render :update do |page|
        page.remove "artwork_description_#{params[:id]}_row"
        page.visual_effect :highlight, 'artwork_description_table', :duration => 2
      end }
    end
  end
  
  def add_ajax
    @artwork_description = ArtworkDescription.new(params[:artwork_description])
    @artwork_description.artwork_id = params[:artwork_id]
    respond_to do |format|
      if @artwork_description.save
        format.js { render :update do |page|
          page.insert_html :bottom, "artwork_description_table", {:partial => "admin/artwork_descriptions/edit_description", :object => @artwork_description}
          page.visual_effect :highlight, "artwork_description_table", :duration => 2
          page[:artwork_description_description].value = ''
          page[:artwork_description_language].value = ''
        end }

      else
        #error
        format.js { render :text => 'error'}
      end
    end
  end
  
  def update_ajax
    @artwork_description = ArtworkDescription.find(params[:id])
    respond_to do |format|
      if @artwork_description.update_attributes(params[:artwork_description])
        format.js { render :update do |page|
          page.visual_effect :highlight, 'description_' + @artwork_description.id.to_s
        end }
      else
        # TODO: error
        format.js { render :update do |page|
          
        end }
      end
    end
  end
end
